﻿#region Using statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace RhythmComet.Services
{
    public class GameScore
    {
        #region Fields and properties
        private int totalRedObstacles;
        private int totalBlueObstacles;
        private int totalGreenObstacles;
        private int totalYellowObstacles;

        private int redScore;
        private int blueScore;
        private int greenScore;
        private int yellowScore;

        private int faults;
        private Color lastColor;
        private int multiplier;
        private int totalScore;
        const int points = 10;

        public float RedPercentage
        {
            get { 
                float result = 100.0f * (float)redScore / (float)totalRedObstacles;
                if (float.IsNaN(result))
                    return 0.0f;
                return result;
            }
        }

        public float GreenPercentage
        {
            get {
                float result = 100.0f * (float)greenScore / (float)totalGreenObstacles;
                if (float.IsNaN(result))
                    return 0.0f;
                return result;
            }
        }

        public float BluePercentage
        {
            get {
                float result = 100.0f * (float)blueScore / (float)totalBlueObstacles;
                if (float.IsNaN(result))
                    return 0.0f;
                return result;
            }
        }

        public float YellowPercentage
        {
            get {
                float result = 100.0f * (float)yellowScore / (float)totalYellowObstacles;
                if (float.IsNaN(result))
                    return 0.0f;
                return result;
            }
        }

        public float FaultPercentage
        {
            get {
                float result = 100.0f * ((float)faults / (float)
                    (redScore + greenScore + blueScore + yellowScore + faults));
                if (float.IsNaN(result))
                    return 0.0f;
                return result;
            }
        }

        public int TotalRedObstacles
        {
            get { return totalRedObstacles; }
            set { totalRedObstacles = value; }
        }

        public int TotalGreenObstacles
        {
            get { return totalGreenObstacles; }
            set { totalGreenObstacles = value; }
        }

        public int TotalBlueObstacles
        {
            get { return totalBlueObstacles; }
            set { totalBlueObstacles = value; }
        }

        public int TotalYellowObstacles
        {
            get { return totalYellowObstacles; }
            set { totalYellowObstacles = value; }
        }

        public int Multiplier
        {
            get { return multiplier; }
        }

        public int RedScore
        {
            get { return redScore; }
            set { redScore = value; }
        }

        public int BlueScore
        {
            get { return blueScore; }
            set { blueScore = value; }
        }

        public int GreenScore
        {
            get { return greenScore; }
            set { greenScore = value; }
        }

        public int YellowScore
        {
            get { return yellowScore; }
            set { yellowScore = value; }
        }

        public int TotalScore
        {
            get { return this.totalScore; }
        }

        public int Faults
        {
            get { return faults; }
            set { faults = value; }
        }
        #endregion

        #region Initializers
        public GameScore()
        {
            Reset();
        }

        public void Reset()
        {
            this.redScore = 0;
            this.blueScore = 0;
            this.greenScore = 0;
            this.yellowScore = 0;
            this.totalRedObstacles = 0;
            this.totalBlueObstacles = 0;
            this.totalGreenObstacles = 0;
            this.totalYellowObstacles = 0;
            this.faults = 0;
            this.lastColor = Color.Red;
            this.multiplier = 1;
            this.totalScore = 0;
            this.lastColor = Color.Red;
        }
        #endregion

        #region Public methods
        public void IncreaseScore(Color color)
        {
            if (color == lastColor)
            {
                if (multiplier < 6)
                    multiplier++;
            }
            else
            {
                lastColor = color;
                multiplier = 1;
            }
            totalScore += multiplier * points;

            if (color == Color.Red)
            {
                RedScore++;
            }
            else if (color == Color.Blue)
            {
                BlueScore++;
            }
            else if (color == Color.Green)
            {
                GreenScore++;
            }
            else
            {
                YellowScore++;
            }
        }

        public void DecreaseScore(Color color)
        {
            lastColor = color;
            multiplier = 1;
            if (totalScore > 0)
            {
                totalScore -= points;
            }
        }

        public string DisplayScore()
        {
            String message = string.Format("Multiplier: {0}  |  Total Score: {1}  |  Faults: {2}",
                this.Multiplier,
                this.TotalScore,
                this.Faults);

            return message;
        }

        public void AddObstacleColor(Color color)
        {
            if (color == Color.Red)
            {
                TotalRedObstacles++;
            }
            else if (color == Color.Blue)
            {
                TotalBlueObstacles++;
            }
            else if (color == Color.Green)
            {
                TotalGreenObstacles++;
            }
            else
            {
                TotalYellowObstacles++;
            }
        }
        #endregion
    }
}
